package com.zkteco.android.IDReader;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import com.zkteco.android.biometric.core.device.ParameterHelper;
import com.zkteco.android.biometric.core.device.TransportType;
import com.zkteco.android.biometric.core.utils.LogHelper;
import com.zkteco.android.biometric.module.idcard.IDCardReader;
import com.zkteco.android.biometric.module.idcard.IDCardReaderFactory;
import com.zkteco.android.biometric.module.idcard.exception.IDCardReaderException;
import com.zkteco.android.biometric.module.idcard.meta.IDCardInfo;
import com.zkteco.android.biometric.module.idcard.meta.IDPRPCardInfo;

import java.util.HashMap;
import java.util.Map;

import static java.lang.Thread.sleep;

public class IDCardRead {

    private Boolean DEBUG = false;

    private static final int VID = 1024;    //IDR VID
    private static final int PID = 50010;     //IDR PID
    private IDCardReader idCardReader = null;

    public boolean isStopIDread=true;
    private boolean isOpenDevice;

    private Bitmap mIDCardBitmap;
    private String LastIDNumber = "";

    private getIDCardInfoListen mLister;

    private Context mContext;
    public IDCardRead(Context context) {
        mContext = context;
        startIDCardReader();
    }

    private void startIDCardReader() {
        // Define output log level
        LogHelper.setLevel(Log.VERBOSE);

        Map idrparams = new HashMap();
        idrparams.put(ParameterHelper.PARAM_KEY_VID, VID);
        idrparams.put(ParameterHelper.PARAM_KEY_PID, PID);
        idCardReader = IDCardReaderFactory.createIDCardReader(mContext, TransportType.USB, idrparams);
    }

    private void readIDCardInfo() {
        try {
            findAndSelect();

            if(idCardReader == null) return;

            int ret = idCardReader.readCardEx(0, 1);
            // 中国大陆居民身份证
            if (1 == ret) {

                IDCardInfo idCardInfo = idCardReader.getLastIDCardInfo();

                if (idCardInfo.getPhotolength() > 0) {
                    byte[] buf = new byte[WLTService.imgLength];
                    if (1 == WLTService.wlt2Bmp(idCardInfo.getPhoto(), buf)) {
                        mIDCardBitmap = IDPhotoHelper.Bgr2Bitmap(buf);
                    }
                }

                if (mLister != null && !idCardInfo.getId().equals(LastIDNumber)) {
                        mLister.getIDCardInfo(ret, idCardInfo, null, null, mIDCardBitmap);
                        LastIDNumber = idCardInfo.getId();
                }
            }
            // 外国人永居证
            else if (2 == ret) {
                if (DEBUG) {
                    Log.d("yanjl", "外国人永居证");
                }

                IDPRPCardInfo idprpCardInfo = idCardReader.getLastPRPIDCardInfo();

                if (idprpCardInfo.getPhotolength() > 0) {
                    byte[] buf = new byte[WLTService.imgLength];
                    if (1 == WLTService.wlt2Bmp(idprpCardInfo.getPhoto(), buf)) {
                        mIDCardBitmap = IDPhotoHelper.Bgr2Bitmap(buf);
                    }
                }

                if (mLister != null && !idprpCardInfo.getId().equals(LastIDNumber)) {
                    mLister.getIDCardInfo(ret, null, idprpCardInfo, null, mIDCardBitmap);
                    LastIDNumber = idprpCardInfo.getId();
                }
            }
            // 港澳台居住证
            else if (3 == ret) {
                if (DEBUG) {
                    Log.d("yanjl", "港澳台居住证");
                }

                IDCardInfo idCardInfo = idCardReader.getLastIDCardInfo();

                if (idCardInfo.getPhotolength() > 0) {
                    byte[] buf = new byte[WLTService.imgLength];
                    if (1 == WLTService.wlt2Bmp(idCardInfo.getPhoto(), buf)) {
                        mIDCardBitmap = IDPhotoHelper.Bgr2Bitmap(buf);
                    }
                }

                if (mLister != null && !idCardInfo.getId().equals(LastIDNumber)) {
                    mLister.getIDCardInfo(ret, null, null, idCardInfo, mIDCardBitmap);
                    LastIDNumber = idCardInfo.getId();
                }
            }
            else {
                LastIDNumber = " ";
                if (DEBUG) {
                    Log.d("yanjl", "读卡失败");
                }
            }
        } catch (IDCardReaderException e) {
            LastIDNumber = " ";
            if (DEBUG) {
                Log.d("yanjl", "未感应到身份证。。");
                e.printStackTrace();
            }
        }
    }

    private void findAndSelect() {
        try {
            idCardReader.findCard(0);
        } catch (IDCardReaderException e) {
            if (DEBUG) {
                e.printStackTrace();
            }
        }

        try {
            idCardReader.selectCard(0);
        } catch (IDCardReaderException e) {
            if (DEBUG) {
                e.printStackTrace();
            }
        }
    }

    class ThreadShow implements Runnable {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (!isStopIDread) {
                try {
                    sleep(300);
                } catch (InterruptedException e) {
                    if (DEBUG) {
                        e.printStackTrace();
                    }
                }
                readIDCardInfo();
            }
        }
    }

    public boolean startIDThread() {
        LastIDNumber = " ";

        if (!isStopIDread) {
            return true;
        }
            
        if (isOpenDevice) {
            isStopIDread = false;
            new Thread(new ThreadShow()).start();
            return true;
        } else {
            return false;
        }
    }

    public void stopIDThread() {
        isStopIDread = true;
    }

    public boolean openDevice() {
        try {
            idCardReader.open(0);
            isOpenDevice = true;
        } catch (IDCardReaderException e) {
            if (DEBUG) {
                e.printStackTrace();
            }
            isOpenDevice = false;
        }
        return isOpenDevice;
    }

    public boolean closeDevice() {
        try {
            idCardReader.close(0);
            isOpenDevice = false;
            isStopIDread = true;
            return true;
        } catch (IDCardReaderException e) {
            if (DEBUG) {
                e.printStackTrace();
            }
            isOpenDevice = true;
            return false;
        }
    }

    public String getSAMID() {
        if(!isOpenDevice && idCardReader == null) {
            return null;
        }

        try {
            return idCardReader.getSAMID(0);
        } catch (IDCardReaderException e) {
            if (DEBUG) {
                e.printStackTrace();
            }
            return null;
        }
    }

    public void setIDCardLister(getIDCardInfoListen lister) {
        mLister = lister;
    }
    public interface getIDCardInfoListen{
        void getIDCardInfo(final int type,
                           final IDCardInfo idcardinfo,
                           final IDPRPCardInfo idprpCardInfo,
                           final IDCardInfo idCardInfo,
                           final Bitmap idcardBitmap);
    }
}
